package com.douguo.recipe.sina;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

import android.net.Uri;

public class Param {

	private HashMap<String, String> map = new HashMap<String, String>();

	public Param() {
	}

	public Param append(String key, String value) {
		return append(key, value, null);
	}

	public Param append(String key, String value, String encoding) {
		if (encoding != null && !encoding.equals("")) {
			value = Uri.encode(value);
		}
		map.put(key, value);
		return this;
	}

	public Param append(Param param) {
		HashMap<String, String> paramMap = param.toMap();

		Iterator<Entry<String, String>> iter = paramMap.entrySet().iterator();
		while (iter.hasNext()) {
			Entry<String, String> entry = iter.next();
			String key = (String) entry.getKey();
			String val = (String) entry.getValue();

			map.put(key, val);
		}

		return this;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();

		Iterator<Entry<String, String>> iter = map.entrySet().iterator();
		while (iter.hasNext()) {
			Entry<String, String> entry = iter.next();	
			String key = (String) entry.getKey();
			String val = (String) entry.getValue();

			sb.append(key + "=" + val + "&");
		}

		return sb.toString();
	}

	public HashMap<String, String> toMap() {
		return map;
	}
}
